package foodmaterial.service.impl;

import foodmaterial.entity.DocumentTrack;
import foodmaterial.entity.SysUser;
import foodmaterial.utils.Constants;
import foodmaterial.utils.DateUtil;
import foodmaterial.utils.SnowflakeWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

public class BaseService {

    @Autowired
    private SnowflakeWorker worker ;

    protected SysUser getSysUser() {
         return (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    protected String getCurrentDate() {
        return DateUtil.format(new Date(), Constants.DEFAULT_DATE_FORMAT) ;
    }

    protected String getDate(Date date) {
        return DateUtil.format(date, Constants.DEFAULT_DATE_FORMAT) ;
    }

    protected boolean isKitchen() {
        return this.getSysUser().getFoodSupplyType().equals(Constants.KITCHEN_FOOD) ;
    }

    protected DocumentTrack getDocumentTrack(Long documentId,
              int documentType, StringBuilder sb) {
        DocumentTrack track = new DocumentTrack() ;
        track.setId(worker.nextId());
        track.setDocumentId(documentId);
        track.setDocumentType(documentType);
        track.setUpdateTime(new Date());
        SysUser userDetails = getSysUser() ;
        track.setUpdatePerson(userDetails.getId());
        track.setUpdatePersonName(userDetails.getName());
        sb.deleteCharAt(sb.length() - 1) ;
        track.setContent(sb.toString());
        return track ;
    }

    protected long nextId() {
        return worker.nextId() ;
    }

    private String executeGetName(Object checkObject) {
        try {
            Method m = checkObject.getClass().getDeclaredMethod("getName",null) ;
            return (String)m.invoke(checkObject,null) ;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException(e) ;
        }
    }

    private Long executeGetId(Object checkObject) {
        try {
            Method m = checkObject.getClass().getDeclaredMethod("getId",null) ;
            return (Long)m.invoke(checkObject,null) ;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException(e) ;
        }
    }

    protected String checkExist(List<?> list,Object checkObject) {
        Long id = executeGetId(checkObject) ;
        String name = executeGetName(checkObject) ;
        if (id == null)
            for (Object o : list) {
                if (name.equals(executeGetName(o)))
                    return name + " 已经存在" ;
            }
        else
            for (Object o : list) {
                if (name.equals(executeGetName(o)) && id.longValue() != executeGetId(o).longValue())
                    return name + " 已经存在" ;
            }
        return null ;
    }
}
